package com.example.meimessage;

import java.util.ArrayList;
import java.util.HashMap;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;



public class ContactsAdapter extends BaseAdapter {
	Context mContext = null;
	@SuppressLint("UseSparseArrays")
	private static HashMap<Integer, Boolean> isSelected= new HashMap<Integer, Boolean>();
	private ArrayList<HashMap<String,Object>> list=new ArrayList<HashMap<String,Object>>();
	private LayoutInflater inflater = null;
	
	public ContactsAdapter(ArrayList<HashMap<String,Object>> list,Context context) {
		this.list=list;
		mContext = context;
		inflater = LayoutInflater.from(context);
		for (int i = 0; i < list.size(); i++) {
			getIsSelected().put(i, false);
		}
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		ViewHolder holder=null;
		if (convertView == null) {
			holder=new ViewHolder();
			convertView = inflater.inflate(
					R.layout.contacts_list, null);

			holder.name = (TextView) convertView.findViewById(R.id.contacts_list_name);
			holder.checkBox = (CheckBox) convertView.findViewById(R.id.contacts_list_check);
			holder.phoneNumber = (TextView) convertView.findViewById(R.id.contacts_list_number);
			
			convertView.setTag(holder);
		}
		else
		{
			holder = (ViewHolder) convertView.getTag();
		}
		
		holder.name.setText(list.get(position).get("name").toString());
		holder.phoneNumber.setText(list.get(position).get("number").toString());
		holder.checkBox.setChecked(getIsSelected().get(position));
		return convertView;
	}
	public static HashMap<Integer, Boolean> getIsSelected() {
		return isSelected;
	}
	public static class ViewHolder
	{
		private TextView name;
		CheckBox checkBox;
		private TextView phoneNumber;
	}
	
}
